const connection = require("../app/database");

class FileService {
  // 增加头像数据
  async insertAvatar(ctx) {
    const filename = ctx.request.file.filename;
    const mimetype = ctx.request.file.mimetype;
    const size = ctx.request.file.size;
    const user_id = ctx.user.id;
    try {
      const statement = `INSERT INTO user_avatar (filename, mimetype, size, user_id) VALUES (?, ?, ?, ?);`;
      await connection.execute(statement, [filename, mimetype, size, user_id]);
    } catch (err) {
      console.log(err);
    }
  }
  // 获取头像信息
  async findAvatarByUserId(userId) {
    const statement = `SELECT * FROM user_avatar WHERE user_id = ?;`;
    const [res, fields] = await connection.execute(statement, [userId]);
    return res[0];
  }
  // 更新头像数据
  async updateAvatar(ctx) {
    const { filename, mimetype, size } = ctx.request.file;
    const user_id = ctx.user.id
    const statement = `UPDATE user_avatar SET filename=?, mimetype=?, size=? WHERE user_id=?;`;
    await connection.execute(statement, [filename, mimetype, size, user_id])
  }
}

module.exports = new FileService();
